import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 读取数据
def load_data(file_path):
    try:
        # 使用 pandas 读取 CSV 文件
        data = pd.read_csv(file_path)
        print(f"成功读取文件: {file_path}")
        return data
    except Exception as e:
        print(f"读取文件时出错: {file_path}, 错误信息: {e}")
        return None

# 主程序
if __name__ == "__main__":
    # 读取数据
    data_300 = load_data("./sensitivity_analysis/data/predictions_300.csv")
    data_310 = load_data("./sensitivity_analysis/data/predictions_310.csv")
    data_320 = load_data("./sensitivity_analysis/data/predictions_320.csv")
    data_330 = load_data("./sensitivity_analysis/data/predictions_330.csv")
    data_340 = load_data("./sensitivity_analysis/data/predictions_340.csv")
    data_350 = load_data("./sensitivity_analysis/data/predictions_350.csv")
    data_360 = load_data("./sensitivity_analysis/data/predictions_360.csv")
    data_370 = load_data("./sensitivity_analysis/data/predictions_370.csv")
    data_380 = load_data("./sensitivity_analysis/data/predictions_380.csv")
    data_390 = load_data("./sensitivity_analysis/data/predictions_390.csv")
    data_400 = load_data("./sensitivity_analysis/data/predictions_400.csv")

    # 检查数据是否成功读取
    if all(data is not None for data in [data_300, data_310, data_320, data_330, data_340, data_350, data_360, data_370, data_380, data_390, data_400]):
        # 合并数据
        combined_data = pd.concat([
            data_300.assign(num_programs=300),
            data_310.assign(num_programs=310),
            data_320.assign(num_programs=320),
            data_330.assign(num_programs=330),
            data_340.assign(num_programs=340),
            data_350.assign(num_programs=350),
            data_360.assign(num_programs=360),
            data_370.assign(num_programs=370),
            data_380.assign(num_programs=380),
            data_390.assign(num_programs=390),
            data_400.assign(num_programs=400)
        ], ignore_index=True)

        # 计算相关性
        correlation = combined_data['num_programs'].corr(combined_data['Progress'])
        print(f"相关系数: {correlation}")

        # 绘制散点图
        plt.scatter(combined_data['num_programs'], combined_data['Progress'])
        plt.title('Scatter Plot of num_programs vs. Progress')
        plt.xlabel('num_programs')
        plt.ylabel('Progress')
        plt.show()

        # 线性回归
        slope, intercept = np.polyfit(combined_data['num_programs'], combined_data['Progress'], 1)
        print(f"线性关系方程: Progress = {slope} * num_programs + {intercept}")
    else:
        print("部分数据读取失败，请检查文件路径和文件格式。")